package com.carrier.springboot.config.shiro;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import com.carrier.springboot.service.IUserService;

/**
 * 
 * @ClassName: MyShiroRealm
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author Carrier
 * @date 2018年9月13日
 *
 */
public class MyShiroRealm extends AuthorizingRealm {

	@Autowired
	private IUserService userService;

	/**
	 * 授权
	 */
	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
		SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
		return authorizationInfo;
	}

	/**
	 * 登录认证
	 */
	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
		String username = token.getPrincipal().toString();

		String password = userService.getPasswordByUsername(username);
		if (password != null) {
			AuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(username, // 认证通过后，存放在session,一般存放user对象
					password, // 用户数据库中的密码
					getName()); // 返回Realm名
			return authenticationInfo;
		}
		return null;
	}

}
